// pagesEnterprise/pages/parkApprove/parkApprove.js
import ajax from '../../../utils/index'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    clientCurrent: 0,
    client: ['业主', '二手房东', '物业', '开发商'],
    typeCurrent: 0,
    type: ['厂房', '土地', '写字楼', '仓库', '商铺'],
    form: {
      bailor: '',
      putType: '',
      name: '',
      phone: '',
    },
    userInfo: '',
    agreement: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    this.get_statistics_num()
    this.setData({
      ['form.bailor']: that.data.client[that.data.clientCurrent],
      ['form.putType']: that.data.type[that.data.typeCurrent],
      userInfo: app.globalData.userInfo,
    })
  },
  /**
     * 协议事件
     */
  agreementChange(event) {
    this.data.agreement = event.detail
  },
  /**
   * 接口 - 获取园区申请入驻人数
   */
  get_statistics_num: function () {
    let that = this
    ajax.get('/wx/appletMessage/statistics').then(res => {
      if (res.code == 200) {
        this.setData({
          statisticsNum: res.data.toString().split('')
        })
      }
    })
  },

  /**
   * 事件 - clientTabChange改变
   */
  clientTabChange: function (e) {
    console.log(e.currentTarget)
    this.setData({
      clientCurrent: e.currentTarget.id,
      ['form.bailor']: e.currentTarget.dataset.text
    })
  },

  /**
   * 事件 - 投放类型tab改变
   */
  typeTabChange: function (e) {
    this.setData({
      typeCurrent: e.currentTarget.id,
      ['form.putType']: e.currentTarget.dataset.text
    })
  },

  /**
   * 获取手机号
   */
  getPhoneNumber: function (e) {
    let that = this
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo.wxPhone) {
      return
    }
    wx.login({
      success(res) {
        if (res.code) {
          const appId = app.globalData.appId
          ajax.get(`/wx/user/${appId}/phone`, {
            appid: app.globalData.appId,
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            code: res.code
          }, app.globalData.appid).then(res => {
            if (res.code == 200) {
              that.setData({
                phoneNumber: res.data.phoneNumber
              })
              app.globalData.userInfo.wxPhone = res.data.phoneNumber
            } else {
              wx.showToast({
                title: '授权登陆失败，请稍后重试',
                icon: 'none'
              })
            }
          })
        }
      }
    })
  },

  /**
   * 表单提交
   */
  submit: function (e) {
    if (!this.data.agreement) return wx.showToast({
      title: '请同意并阅读盈联易单配服务协议',
      icon: 'none'
    })
    let value = e.detail.value
    if (!value.name) {
      wx.showToast({
        title: '请填写姓名',
        icon: 'none'
      })
      return
    }
    if (!value.phone) {
      wx.showToast({
        title: '请填写联系方式',
        icon: 'none'
      })
      return
    }
    if (!value.gardenName) {
      wx.showToast({
        title: '请填写园区名称',
        icon: 'none'
      })
      return
    }
    let form = this.data.form
    form.name = value.name
    form.phone = value.phone
    form.gardenName = value.gardenName
    const userInfo = wx.getStorageSync('userInfo')
    form.wxUserId = userInfo.id
    console.log(form)
    ajax.post('/wx/appletMessage/add',form).then(async(res) => {
      if (res.code == 200) {
       await ajax.showSuccess('提交成功')
       wx.navigateBack()
      } else {
        wx.showToast({
          title: '提交失败，请稍后重试',
          icon: 'none'
        })
      }
    })
  },
})